﻿// const xlsx = require('node-xlsx')
const fs = require('fs')
const iconv = require('iconv-lite')
function func(data) {
    console.log('Line: ' + data);
}

fs.readdir('./input', {
    encoding: 'utf8'
}, function (err, files) {
    files.forEach(file => {
        let path = `${__dirname}/input/${file}`
        console.log(path)

        var input = fs.createReadStream(path, {
            start: 0
        }).pipe(iconv.decodeStream('gb2312'));

        var output = fs.createWriteStream(`${__dirname}/output/${file}`);
        output.write(new Buffer('\xEF\xBB\xBF','binary'))
        var remaining = '';
        var nexthours = null
        var nextmin = null
        var nextsec = null
        
        input.on('data', function (data) {
            remaining += data;
            var index = remaining.indexOf('\n');
            var last = 0;

            while (index > -1) {
                var line = remaining.substring(last, index);
                let rowdata = line.split(",")
                let begindata = rowdata[rowdata.length - 1]
                let curhour = new Date(begindata).getHours()
                let curmin = new Date(begindata).getMinutes()
                let cursec = new Date(begindata).getSeconds()
                last = index + 1;

                if (isNaN(curhour) || (nexthours == null && nextmin == null && nextsec == null)) {
                    
                    console.log(line)
                    output.write(line+'\n')
                    if (!isNaN(curhour)) {
                        nextmin = curmin - 10
                        nexthours = curhour
                        if (nextmin < 0) {
                            nextmin += 60
                            nexthours = curhour - 1
                        }
                        if (nexthours < 0) {
                            nexthours += 24
                        }
                        nextsec = cursec
                    }
                } else {
                    if (nextmin == curmin && nextsec == cursec && nexthours == curhour) {
                        console.log(line)
                        output.write(line+'\n')
                        nextmin = curmin - 10
                        nexthours = curhour
                        if (nextmin < 0) {
                            nextmin += 60
                            nexthours = curhour - 1
                        }
                        if (nexthours < 0) {
                            nexthours += 24
                        }
                        nextsec = cursec
                    }
                }
              
                index = remaining.indexOf('\n', last)

            }

            remaining = remaining.substring(last);
        });

        input.on('end', function () {
            if (remaining.length > 0) {
                func(remaining);
                output.end()
            }
        });
        // let sheetlist = xlsx.parse(path, { cellDates: true })
        // let filterlist = {
        //     name: 'Sheet1',
        //     data: []
        // }
        // sheetlist.forEach((sheet) => {
        //     // console.log(sheet)
        //     let data = sheet.data
        //     data.forEach((row, index) => {
        //         if (index == 0) {
        //             console.log(row)
        //             // filterlist.data.push(row)
        //             filterlist.data.push([
        //                 '测点编号', '缝宽', '采集时间'
        //             ])
        //         } else {
        //             if (index % (140 * 60 * 10) == 0) {
        //                 let date = row[2]
        //                 let row3 = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
        //                 filterlist.data.push([row[0],
        //                 row[1],
        //                     row3
        //                 ])
        //             }
        //         }
        //     })
        // })
        // console.log(filterlist)
        // let buffer = xlsx.build([filterlist]);
        // fs.writeFile(`${__dirname}/output/${file}`, buffer, 'utf8', function (err) {
        //     if (err) {
        //         console.log(err)
        //         return;
        //     }
        // })
    })
})